package com.example.take_out_admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_admin.common.Result;
import com.example.take_out_admin.common.lang.dto.EmployeeDto;
import com.example.take_out_admin.common.lang.dto.EmployeePageQueryDTO;
import com.example.take_out_admin.common.lang.vo.BaseVo;
import com.example.take_out_admin.entity.Employee;
import com.example.take_out_admin.service.EmployeeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author FZ
 * @since 2024-06-29
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public Result login(@RequestBody EmployeeDto employeeDto) {
        return employeeService.login(employeeDto);
    }

    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Employee> findById(@PathVariable("id") String id) {
        Employee employee = employeeService.getById(id);
        employee.setPassword(null);
        return Result.success(employee);
    }

    @PostMapping("/page")
    public Result<IPage<Employee>> page(@RequestBody EmployeePageQueryDTO source) {
        log.info("分页: {}", source);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(source.getName()), Employee::getName, source.getName());
        IPage<Employee> pageInfo = employeeService.page(
                new Page<>(source.getPage(), source.getPageSize()), wrapper
        );
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result<Object> save(@RequestBody Employee employee){
        employeeService.saveEmployee(employee);
        return Result.success();
    }

    @PutMapping
    public Result<Object> update(@RequestBody Employee employee){
        this.employeeService.updateEmployee(employee);
        return Result.success();
    }


    /**
     * 启用/禁用用户
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}/{id}")
    public Result startOrStop(@PathVariable("status") Integer status,
                              @PathVariable("id") String id) {
        employeeService.startOrStop(status,id);
        return Result.success();
    }
}
